package cn.gson.springboot.model.service;

import java.math.BigDecimal;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.DoctoradviceDetailDAO;
import cn.gson.springboot.model.dao.OutpatientstorageDao;
import cn.gson.springboot.model.dao.PublicDurgDao;
import cn.gson.springboot.model.dao.PublicdurgDetailDao;
import cn.gson.springboot.model.mapper.OutpatientstorageMapper;
import cn.gson.springboot.model.pojos.DoctoradviceDetail;
import cn.gson.springboot.model.pojos.Drug;
import cn.gson.springboot.model.pojos.DrugApply;
import cn.gson.springboot.model.pojos.DrugapplyDetail;
import cn.gson.springboot.model.pojos.PublicDurg;
import cn.gson.springboot.model.pojos.PublicdurgDetail;
import cn.gson.springboot.model.vo.DrugfyVo;
import cn.gson.springboot.model.vo.DrugqlVo;

@Service
@Transactional(rollbackOn=Exception.class)
public class OutpatientstorageService {
	@Autowired
	private OutpatientstorageMapper mzkcmapper;
	
	@Autowired
	private OutpatientstorageDao mzkcdao;
	
	@Autowired
	private PublicDurgDao fydao;
	
	@Autowired
	private PublicdurgDetailDao fyxxdao;
	
	@Autowired
	private DoctoradviceDetailDAO yzdao;
	
	
	//查询所有门诊库存
	public JSONArray selectAllzmck() {
		return mzkcmapper.selectAllmzkc();
	}
	
	//显示所有发药信息
	public JSONArray selectAllDrugmzfy() {
		return mzkcmapper.selectAllDrugmzfy();
	}
	
	//查询所有盘点库存
	public JSONArray selectAllpandianxx() {
		return mzkcmapper.selectAllpandianxx();
	}
	
	//显示所有新增盘点药品详情信息
	public JSONArray selectAllpandianxzxx() {
		return mzkcmapper.selectAllpandianxzxx();
	}
	
	//查询所有药品信息
	public JSONArray mzkcmapper(String drugname) {		
			return mzkcmapper.selectAllmzkc(drugname);
	}
		
	//显示门诊医疗卡
	public JSONArray selectAllmzylkxs() {
			return mzkcmapper.selectAllmzylkxs();
	}	

	//显示住院医疗卡
	public JSONArray selectAllzyylkxs() {
			return mzkcmapper.selectAllzyylkxs();
	}
	
	//显示住院记录详情
	public JSONArray selectAllzyjlxq(Integer pid) {
			return mzkcmapper.selectAllzyjlxq(pid);
	}
	
	//搜索门诊医疗卡信息
	public JSONArray selectAllmzylk(Integer cliniccardid) {		
			return mzkcmapper.selectAllmzylk(cliniccardid);
	}
	//搜索住院医疗卡信息
	public JSONArray selectAllzyylk(Integer cliniccardid) {		
			return mzkcmapper.selectAllzyylk(cliniccardid);
	}
	
	//搜索门诊医疗卡信息
	public JSONArray selectAllDrugzyfycx(Integer publicdurgid) {		
			return mzkcmapper.selectAllDrugzyfycx(publicdurgid);
	}
	
	//显示医疗卡详情药品信息
	public JSONArray selectAllzyylkxq(Integer cid) {
			return mzkcmapper.selectAllzyylkxq(cid);
		}
	
	//显示门诊医疗卡详情药品信息
	public JSONArray selectAllmzylkxq(Integer cid) {
		return mzkcmapper.selectAllmzylkxq(cid);
	}
	
	//显示请领记录的详情信息
	public JSONArray selectAllqlxq(Integer drugid) {
		return mzkcmapper.selectAllqlxq(drugid);
		
	
	}
	
	//药品药库的新增
	public void addDrugyaokuxz(String healthno_ru,Integer drugnumber,Integer outpatientpici,
			Integer outpatientstorageamount) {
	
		//入库药品编号
		String arr[] =healthno_ru.split(",");
		if(!"".equals(healthno_ru)) {
			for(int i=0;i<arr.length;i++){
				 System.out.println(arr[i]+"--------遍历health_no");
				 mzkcmapper.addDrugyaokuxz(Integer.parseInt(arr[i]), outpatientpici, outpatientstorageamount);
			}    
		}
	}
	
	//药品的数量增加
	public JSONArray selectAllqlxq(Integer outpatientstorageid,Integer sum) {
		return mzkcmapper.selectAllkucuntj(outpatientstorageid,sum);
	
	}
	
		//药品药库的新增
		public void addDrugyaozx(String healthno_fa,Integer publicdurg_id,Integer publicdurgshuliang,Integer publicdurgshijishuliang,
				Integer publicdurgtype,BigDecimal publicdurgallmoney,String publicdurgpeope,String publicdurglingyaopeople) {
		
			//入库药品编号
			String arr[] =healthno_fa.split(",");
			if(!"".equals(healthno_fa)) {
				for(int i=0;i<arr.length;i++){
					 System.out.println(arr[i]+"--------遍历health_no");
					 mzkcmapper.addDrugyaozx(Integer.parseInt(arr[i]),publicdurgshuliang, publicdurgshijishuliang, publicdurgtype, publicdurgallmoney, publicdurgpeope, publicdurglingyaopeople);
				}    
			}
		}
			
		

		
		//新发药记录
				public void DrugIndent(DrugfyVo drugfyVo) {
					//获取药品信息进行新增
					PublicDurg publicdurg = drugfyVo.getPublicdurg();
					
					PublicDurg save = fydao.save(publicdurg);
					
					List<PublicdurgDetail> details = drugfyVo.getPublicdurgdetail();
					
					//DoctoradviceDetail doc = drugfyVo.getDoc();
					
					//循环
					for (PublicdurgDetail pur : details) {
					//添加发药信息
					pur.setPublicDurg(publicdurg);
					System.out.println("id="+pur.getDoctoradviceDetail().getDocdetailsId());
					//添加医嘱信息
					DoctoradviceDetail yizhu = yzdao.findById(pur.getDoctoradviceDetail().getDocdetailsId()).get();
					System.out.println(yizhu.getDocdetailsId());
					//重新赋值药品对象
					pur.setDoctoradviceDetail(yizhu);
					fyxxdao.save(pur);
					}
				
				}
			}
